<?php


namespace app\index\controller;

use think\Db;
use think\Exception;

/**
 * 数据库更新相关
 * Class DbUpdateTest
 * @package app\index\controller
 */
class DbUpdateTest
{
    /**
     * update
     * sql：UPDATE `t_user`  SET `name` = '周杰伦001'  WHERE  `id` = 10000025
     * @return \think\response\Json
     */
    public function test_update()
    {
        try {
            $res = Db::table('t_user')
                ->where('id', 10000025)
                ->update(['name' => '周杰伦001']);
            return json(['msg' => '更新成功！', 'data' => $res]);
        } catch (Exception $e) {
            return json('更新异常');
        }
    }

    /**
     * data update
     * sql：UPDATE `t_user`  SET `name` = '朱军002'  WHERE  `id` = 10000030
     * @return \think\response\Json
     */
    public function test_data_update()
    {
        try {
            $res = Db::table('t_user')
                ->where('id', 10000030)
                ->data(['name' => '朱军002'])
                ->update();
            return json(['msg' => '更新成功！', 'data' => $res]);
        } catch (Exception $e) {
            return json('更新异常');
        }
    }

    /**
     * sql 字段增加inc update
     * sql：UPDATE `t_user`  SET `age` = `age` + 1  WHERE  `id` = 10000035
     * @return \think\response\Json
     */
    public function test_inc_update()
    {
        try {
            $res = Db::table('t_user')
                ->where('id', 10000035)
                // 字段增加
                ->inc('age')
                ->update();
            return json(['msg' => '更新成功！', 'data' => $res]);
        } catch (Exception $e) {
            return json('更新异常');
        }
    }

    /**
     * sql 字段减少dec update
     * sql：UPDATE `t_user`  SET `age` = `age` - 2  WHERE  `id` = 10000035
     * @return \think\response\Json
     */
    public function test_dec_update()
    {
        try {
            $res = Db::table('t_user')
                ->where('id', 10000035)
                // 字段减少
                ->dec('age', 2)
                ->update();
            return json(['msg' => '更新成功！', 'data' => $res]);
        } catch (Exception $e) {
            return json('更新异常');
        }
    }

    /**
     * sql 表达式exp update
     * sql：UPDATE `t_user`  SET `name` = UPPER(name)  WHERE  `id` = 10000035
     * @return \think\response\Json
     */
    public function test_exp_update()
    {
        try {
            $res = Db::table('t_user')
                ->where('id', 10000035)
                // 字段减少
                ->exp('name', 'UPPER(name)')
                ->update();
            return json(['msg' => '更新成功！', 'data' => $res]);
        } catch (Exception $e) {
            return json('更新异常');
        }
    }

    /**
     * sql raw update
     * sql：UPDATE `t_user`  SET `name` = LOWER(name) , `age` = age+3  WHERE  `id` = 10000035
     * @return \think\response\Json
     */
    public function test_raw_update()
    {
        try {
            $res = Db::table('t_user')
                ->where('id', 10000035)
                ->update([
                    'name' => Db::raw('LOWER(name)'),
                    'age' => Db::raw('age+3'),
                ]);
            return json(['msg' => '更新成功！', 'data' => $res]);
        } catch (Exception $e) {
            return json('更新异常');
        }
    }

    /**
     * sql raw update
     * sql：UPDATE `t_user`  SET `age` = 25  WHERE  `id` = 10000035
     * @return \think\response\Json
     */
    public function test_filed_update()
    {
        try {
            $res = Db::table('t_user')
                ->where('id', 10000035)
                ->setField('age', '25');
            return json(['msg' => '更新成功！', 'data' => $res]);
        } catch (Exception $e) {
            return json('更新异常' . $e);
        }
    }
}